package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.mapper.DtsRoleMapper;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsRole;
import com.qf.service.DtsRoleService;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 邢式瑞
 * @version 1.0
 * @project E-commercebackGroundManagementSystem
 * @date 2022/9/20 21:38
 */
@Service
public class DtsRoleServiceImpl extends ServiceImpl<BaseMapper<DtsRole>,DtsRole> implements DtsRoleService {

        @Autowired
        private DtsRoleMapper roleMapper;

        /**
         * 根据角色id数组返回对应的角色名称集合
         * @param roleIds
         * @return
         */
        @Override
        public Set<String> queryByIds(Integer[] roleIds) {
            Set<String> roles = new HashSet<String>();
            if (roleIds == null || roleIds.length == 0) {
                return roles;
            }

            //设置查询条件
            QueryWrapper<DtsRole> wapper = new QueryWrapper<>();
            wapper.in("id", Arrays.asList(roleIds));
            wapper.eq("deleted", false);
            wapper.eq("enabled", true);

            //查询
            List<DtsRole> roleList = roleMapper.selectList(wapper);

            for (DtsRole role : roleList) {
                roles.add(role.getName());
            }
          //  Set<String> collect = roleList.stream().map(DtsRole::getName).collect(Collectors.toSet());


            return roles;
        }

    @Override
    public List<CatVo> findCatVoList() {
        List<DtsRole> dtsRoles = roleMapper.selectList(new QueryWrapper<DtsRole>().select("id ,name"));
        return dtsRoles.stream().map(dtsRole -> {
            CatVo catVo = new CatVo();
            catVo.setValue(dtsRole.getId());
            catVo.setLabel(dtsRole.getName());
            return catVo;
        }).collect(Collectors.toList());
    }

    @Override
    public IPage<DtsRole> findRoleList(int page, int limit, String sort, String order) {

        Page<DtsRole> dtsRolePage = new Page<>(page, limit);
        QueryWrapper<DtsRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderBy(!StringUtils.isEmpty(sort),"asc".equalsIgnoreCase(order),sort).eq("deleted",false);


       return roleMapper.selectPage(dtsRolePage,queryWrapper);
    }

    @Override
    public void createRole(DtsRole dtsRole) {
        roleMapper.insert(dtsRole);
    }

    @Override
    public void updateRole(DtsRole dtsRole) {
        roleMapper.updateById(dtsRole);
    }

    @Override
    public void deleteRole(DtsRole dtsRole) {
        roleMapper.updateById(dtsRole);
    }


}


